package com.ch.dream.logic.longstory;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.widget.EditText;
import cn.goso.utility.NumberTools;

import com.ch.dream.R;
import com.ch.dream.activity.longstory.AddStoryActivity;
import com.ch.dream.asynctask.AddLongStoryRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.bean.LongstoryBean;
import com.ch.dream.common.CommonUserInfo;
import com.ch.dream.tools.UtilTools;
import com.ch.dream.tools.ViewTools;

public class AddStoryLogic extends BaseLogic {
	
	private ProgressDialog loadingDialog;
	private EditText etContent;
	private EditText etSummary;
	private int ADDSTORYRESULT = 1;
	public AddStoryLogic(AddStoryActivity activity) {
		super(activity);
	}

	public AddStoryLogic(AddStoryActivity activity, int layoutId) {
		super(activity, layoutId);
	}
	
	public void initViews(){
		etContent =(EditText) activity.findViewById(R.id.etContent);
		etSummary =(EditText) activity.findViewById(R.id.etSummary);
	}
	
	public void init(){
		initViews();
	}
	
	public void back(){
		loadingDialog = ViewTools.showLoading(activity,"提示", "返回中...");
		loadingDialog.dismiss();
		activity.finish();
	}
	
	public void submit(){
		if(validate()){
			String content = etContent.getText().toString();
			String summary = etSummary.getText().toString();
			int userId = NumberTools.stringToInt(CommonUserInfo.userId);
			LongstoryBean bean =new LongstoryBean();
			bean.setContent(content);
			bean.setSummary(summary);
			bean.setOwnerId(userId);
			bean.setCreatorId(userId);
			new AddLongStoryRequestAsyncTask(activity, new RequestServerListener() {
				@Override
				public void onRequestServerBegin() {
					loadingDialog = ViewTools.showLoading(activity,"提示", "提交数据中，请稍候...");
				}
				
				@Override
				public void onRequestServerEnd(Object result) {
					try{
						loadingDialog.dismiss();
						if(result!=null){
							JSONObject datas = (JSONObject)result;
							ViewTools.showShortToast(activity,"添加成功");
							Intent intent =new Intent();
							intent.putExtra("id", datas.getString("id"));
							intent.putExtra("summary", etSummary.getText().toString());
							intent.putExtra("content",  etContent.getText().toString());
							activity.setResult(ADDSTORYRESULT,intent);
							activity.finish();
						}else{
							ViewTools.showShortToast(activity,"网络错误");
						}
					}catch (Exception e) {
						ViewTools.showShortToast(activity,"网络错误");
					}
				}
			}).execute(bean);
		}
	}
	
	public boolean validate(){
		String content = etContent.getText().toString();
		String summary = etSummary.getText().toString();
		if(!UtilTools.isNotNullOrEmpty(summary)){
			ViewTools.showShortToast(activity,"标题不能为空");
			return false;
		}
		if(!UtilTools.isNotNullOrEmpty(content)){
			ViewTools.showShortToast(activity,"内容不能为空");
			return false;
		}
		return true;
	}

}
